package com.zmn.oms.admin.controller.perf.distribute;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto.LayuiTableResponseDTO;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.common.utils.office.ExcelUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.permit.client.annotation.FilterPermitData;
import com.zmn.oms.admin.controller.perf.PerfBaseController;
import com.zmn.oms.business.interfaces.perf.PerfQueryBService;
import com.zmn.oms.model.entity.perf.PerfDimensionDistribute;
import com.zmn.oms.model.entity.perf.PerfDimensionDistributeQuery;
import com.zmn.oms.model.vo.perf.PerfDistributeVO;
import com.zmn.oms.services.interfaces.perf.PerfDimensionDistributeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述:[绩效]派单报表
 *
 * @author wangjie
 * @date 9:56 2019/3/2
 **/
@RestController
@RequestMapping(value = "/perf/distribute/")
public class PerfDistributeListController extends PerfBaseController {

    @Resource
    PerfQueryBService<PerfDistributeVO, PerfDimensionDistribute, PerfDimensionDistributeQuery> perfQueryBService;
    @Resource
    PerfDimensionDistributeService perfDimensionDistributeService;

    /**
     * 描述: 派单报表统计主页
     *
     * @param modelAndView 视图模型返回
     * @return org.springframework.web.servlet.ModelAndView
     * @author wangjie
     * @date 10:01 2019/3/2
     **/
    @RequestMapping(value = "list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("perf/distribute/list");
        return modelAndView;
    }

    /**
     * 描述: 派单报表统计主页 ajax请求
     *
     * @param query 公司绩效统计 查询条件
     * @return com.zmn.common.dto.LayuiTableResponseDTO
     * @author wangjie
     * @date 16:48 2019/3/1
     **/
    @FilterPermitData
    @RequestMapping(value = "loadData")
    @ResponseBody
    public LayuiTableResponseDTO loadData(PerfDimensionDistributeQuery query) {
        List<PerfDistributeVO> perfDistributeVOS = perfQueryBService.listPageByQuery(query);
        LayuiTableResponseDTO<PerfDistributeVO> success = LayuiTableResponseDTO.success(query.getTotalCount(), perfDistributeVOS);
        PerfDistributeVO totalLine = perfQueryBService.getTotalLine(query);
        success.setExtData(totalLine);
        return success;
    }

    /**
     * 描述: 根据条件导出派单Excel
     *
     * @param query 查询条件
     **/
    @FilterPermitData
    @RequestMapping(value = "export")
    public void export(PerfDimensionDistributeQuery query, HttpServletResponse response){
        processingQuery(query);
        LoginStaff currentStaff = getCurrentStaff();
        Integer exportLimit = currentStaff.getExportLimit();
        query.setPageSize(exportLimit);
        query.setPageIndex(1);

        List<PerfDistributeVO> perfDistributeVOS = perfQueryBService.listPageByQuery(query);
        List<DictModel> platList = GlobalConsts.getPlatList();
        Map<Integer, String> platNameMap = platList.stream().collect(Collectors.toMap(DictModel::getKey, DictModel::getValue));
        List<List> exportData = perfDistributeVOS.stream().map(vo -> Arrays.asList(
                // 服务平台
                platNameMap.get(vo.getPlat()),
                // 部门ID
                vo.getDeptId(),
                // 派单员姓名
                vo.getDistributorName(),
                // 确认
                vo.getConfirmCount(),
                // 派单
                vo.getDistributeCount(),
                // 派单过
                vo.getDistributedCount(),
                // 备注
                vo.getRemarkCount(),
                // 备注过
                vo.getRemarkedCount(),
                // 跟单
                vo.getTrackCount(),
                // 跟单过
                vo.getTrackedCount(),
                // 派单成功
                vo.getDistributeSuccessCount(),
                // 异常处理
                vo.getAbnormalHandlingCount(),
                // 投诉处理
                vo.getComplaintHandlingCount()
        )).collect(Collectors.toList());
        exportData.add(0, Arrays.asList("服务平台", "部门ID", "派单员姓名", "确认", "派单", "派单过", "备注", "备注过", "跟单", "跟单过", "派单成功", "异常处理", "投诉处理"));
        try {
            String name = "派单报表.xlsx";
            name = new String(name.getBytes(), "ISO8859-1");
            ExcelUtil.export(response, exportData, name, "sheet", false);
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "导出报表出错" + e.getMessage(), e);
        }
    }

    @ResponseBody
    @RequestMapping(value = "listDistributorByPlat")
    public List<VtDTO> listDistributorByPlat(Integer plat) {
        return this.perfDimensionDistributeService.listDistributorByPlat(plat);
    }
    /**
     * 处理查询条件
     *
     * @param query
     */
    private void processingQuery(PerfDimensionDistributeQuery query) {
        if (Objects.isNull(query.getFrom())) {
            query.setFrom(YearMonth.now().atDay(1));
        }
        if (Objects.isNull(query.getTo())) {
            query.setTo(LocalDate.now());
        }
    }
}
